/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
//Source file: H:\\temp\\generated\\mouseoveraction\\ActionHandleLocator.java

package com.cea.papyrus.core.editor.mouseoveraction;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Locator;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * A locator is responsible of locating the handle around the figure.
 */
public class ActionHandleInsideLocator implements Locator {
	
	/**
	 * Handle location.
	 */
	protected int location;
    
    /**
     * 
     */
	protected IFigure reference;

	/**
	 * 
	 * 
	 * @param location use PositionConstants
	 * @param reference 
	 */
	public ActionHandleInsideLocator(IFigure reference, int location) {
		this.reference = reference;
		this.location = location;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected Rectangle getReferenceBox() {
		return reference.getBounds();
	}

	/**
	 * Relocates the given IFigure.
	 * 
	 * @param target The figure to relocate
	 */
	public void relocate(IFigure target) {
		Rectangle targetBounds = new PrecisionRectangle(getReferenceBox()
				.getResized(-1, -1));
		reference.translateToAbsolute(targetBounds);
		target.translateToRelative(targetBounds);
		targetBounds.resize(1, 1);

		Dimension targetSize = target.getPreferredSize();

		int midx = (targetBounds.width + 1) / 2 - ((targetSize.width + 1) / 2);
		int midy = (targetBounds.height + 1) / 2
				- ((targetSize.height + 1) / 2);
		int dx = midx, dy = midy;
		if ((location & PositionConstants.NORTH)>=1) {
//			dx = midx;
			dy = -5;
			//			dy = -targetSize.height - 2;
		} 
		else if ((location & PositionConstants.SOUTH)>=1) {
//			dx = midx;
			dy = targetBounds.height - targetSize.height + 1;
			//			dy = targetBounds.height + 2;
		}

		if ((location & PositionConstants.WEST)>=1) {
			//			dx = -targetSize.width - 2;
			dx = -1;
//			dy = midy;
		} 
		else if ((location & PositionConstants.EAST)>=1) {
//						dx = targetBounds.width ;
			dx = targetBounds.width - targetSize.width + 1;
//			dy = midy;
		} 
		// else : use default
		targetBounds.x += dx;
		targetBounds.y += dy;

		targetBounds.setSize(targetSize);
		target.setBounds(targetBounds);

	}

}
